#coding=utf-8
import time  
from selenium import webdriver  
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

import math
import random
import numpy as np
import os
import xlrd
import pandas as pd



def batch_update(alert_id="Alert - 1947071", note='MCC updated'):
        
    driver = webdriver.Chrome(r'C:\Users\guosu\Desktop\chromedriver_win32\chromedriver.exe')
    elem = driver.get('https://goto.lightning.force.com/lightning/o/Business_Support_Case__c/list?filterName=00B2E00000AmN9qUAF')
    time.sleep(5)
    elem_search = driver.find_element_by_link_text(alert_id).click()
    print("done!")
    time.sleep(8)
    elem_button = driver.find_element_by_xpath('//*[@title="Edit Alert Status"]').click()
    time.sleep(2)
    actions=ActionChains(driver)

    alert_status = driver.find_element_by_class_name('flexipageComponent')

    a = actions.move_to_element(alert_status)
    a.perform()
    time.sleep(3)
    b = a.send_keys(Keys.TAB)
    for i in range(7):
        b.perform()
        time.sleep(0.05)
    time.sleep(2)
    c = b.send_keys(Keys.TAB).send_keys(Keys.UP).send_keys(Keys.ENTER).send_keys(Keys.ENTER)
    c.perform()
    print("confirmed completed")
    time.sleep(3)
    for i in range(2):
        b.perform()
        time.sleep(0.1)
    time.sleep(2)
    d = b.send_keys(Keys.TAB).send_keys(note).perform()
    print("note lefted")
    time.sleep(2)
    driver.find_element_by_xpath('//*[@title="Save"]').click()
    time.sleep(3)
    driver.quit()



df = pd.read_excel("3.xlsx", sheet_name=0)     # add sheetname in so we can put all data in one excel
alert_id_list = list()
note_list = list()
print(df)
i = 0
while (i < len(df)) :
    alert_id_list.append(df.iloc[i][0])
    note_list.append(df.iloc[i][1])
    i = i + 1
i = 0
print(alert_id_list, note_list)


for a in alert_id_list:
    #b = "https://admin.paypal.com/cgi-bin/admin?node=supplemental_merchant_info&account_number="+str(a)+"#admin_restrictions_new"
    print("Updating {} time, on account {}".format(str(i+1),a))
    try:
        batch_update(alert_id_list[i],note_list[i])
        print("Successful updated!")
    except Exception as e:
        print(e)
    # we can cancel this waitting too
    time.sleep(1)
    i += 1
    # if no need to stop, then active below break function.
    #if i == 20:
        #break
else:
    print("All task completed, please check again manually. Thanks.  -- Yours J" )



